﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using System;

namespace Blueprints.IO
{
    partial class StartNode
    {
        /// <summary>
        /// Gets whether <paramref name="name"/> is a valid name for a blueprint root node.
        /// </summary>
        /// <param name="name">The name to validate.</param>
        /// <returns>True if <paramref name="name"/> is valid, false otherwise.</returns>
        public static bool IsValidName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }

            if (!char.IsLetter(name[0]))
            {
                return false;
            }

            for (int i = 1; i < name.Length; i++)
            {
                char c = name[i];
                if (!char.IsLetterOrDigit(c) && c != '_')
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Returns a valid node name for <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The type of which to get the name.</param>
        /// <param name="default">The default name to return.</param>
        /// <returns>A valid node name for the type.</returns>
        public static string GetValidNodeName(Type type, string @default)
        {
            if (type == null)
            {
                return @default;
            }

            string name = type.Name;
            if (!IsValidName(name))
            {
                return @default;
            }

            return name;
        }

        /// <summary>
        /// Returns a valid node name for <paramref name="assemblyQualifiedName"/>.
        /// </summary>
        /// <param name="assemblyQualifiedName">The assembly qualified name of a type.</param>
        /// <param name="default">The default node name.</param>
        /// <returns>A valid node name for the type.</returns>
        public static string GetValidNodeName(string assemblyQualifiedName, string @default)
        {
            if (string.IsNullOrEmpty(assemblyQualifiedName))
            {
                return @default;
            }

            int commaIndex = assemblyQualifiedName.IndexOf(',');
            int startIndex;
            if (commaIndex < 0)
            {
                commaIndex = assemblyQualifiedName.Length;
                startIndex = 0;
            }
            else
            {
                startIndex = assemblyQualifiedName.LastIndexOf('.', commaIndex) + 1;
            }

            string name = assemblyQualifiedName.Substring(startIndex, commaIndex - startIndex).Trim();
            if (!IsValidName(name))
            {
                return @default;
            }

            return name;
        }
    }
}